<template>
  <div class="goodsFenleiCreate">
    <el-card class="box-card">
      <!-- 头部 -->
      <div slot="header" class="clearfix header">
        <span>用户创建</span>
        <el-button type="primary" round @click="backFn">返回</el-button>
      </div>
      <!-- end---------------- -->

      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off">
          </el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="mobile">
          <el-input type="text" v-model="ruleForm.mobile" autocomplete="off">
          </el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          >
          </el-input>
        </el-form-item>

        <el-form-item label="密保问题" prop="passwd_question">
          <el-select v-model="ruleForm.passwd_question" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="密保答案" prop="passwd_answer">
          <el-input
            type="password"
            v-model="ruleForm.passwd_answer"
            autocomplete="off"
          >
          </el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        <!-- ------------------------------end -->
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { postYonghuCreate } from "@/api";
export default {
  data() {
    return {
      //存放密保问题的变量
      options: [
        { value: "您其中一位老师的名字", label: "您其中一位老师的名字" },
        { value: "您个人计算机的型号", label: "您个人计算机的型号" },
        { value: "您最喜欢的餐馆名称", label: "您最喜欢的餐馆名称" },
        { value: "驾驶执照最后四位数字", label: "驾驶执照最后四位数字" }
      ],
      //表单添加数据
      ruleForm: {
        username: "",
        password: "",
        mobile: "",
        passwd_question: "",
        passwd_answer: "",
        mobile: ""
      },
      rules: {
        //键 ruleForm里面的键
        //值 数组里面是一个个对象 赋值
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        passwd_question: [
          { required: true, message: "请输入密保问题", trigger: "blur" }
        ],
        passwd_answer: [
          { required: true, message: "密保答案", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //提交
    submitForm(refName) {
      //this.$refs[formName].validate((valid) => {}
      this.$refs[refName].validate(isSuccess => {
        if (isSuccess) {
          postYonghuCreate(this.ruleForm).then(res => {
            // console.log(res)
            if (res.meta.state == 201) {
              this.$message.success("操作成功");
              this.$router.push("/users");
            } else {
              this.$message.error(res.meta.msg);
            }
          });
        }
      });
    },
    //重置
    resetForm(refName) {
      //  this.$refs[formName].resetFields();
      this.$refs[refName].resetFields();
    },
    //返回
    backFn() {
      this.$router.push({ path: "/users" });
    }
  }
};
</script>

<style lang="scss" scoped>
.goodsFenleiCreate {
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
